"use strict";

const Controller = require("egg").Controller;

class CarouselGroupController extends Controller {
  /**
     *
     * @api {POST} /api/carouselGroup/create 创建轮播图分组
     * @apiName 创建轮播图分组
     * @apiGroup 轮播图分组模块
     * @apiVersion  1.0.0
     *
     * @apiParam  {String} name 分组名
     *
     * @apiParamExample  {type} Request-Example:
        {
            "name":"banner",
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 2,        //分组id
                "name": "banner",   //分组名
                "projectId": 1,     //项目id
                "adminId": 1,       //管理员id
                "updatedAt": "2021-11-11T09:54:06.752Z",
                "createdAt": "2021-11-11T09:54:06.752Z"
            }
        }
     *
     *
     */
  async create() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, "name").checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.carouselGroup.create();
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule("操作失败");
    }
  }

  /**
     *
     * @api {DELETE} /api/carouselGroup/delete 删除轮播图分组
     * @apiName 删除轮播图分组
     * @apiGroup 轮播图分组模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} id 轮播图分组id
     *
     * @apiParamExample  {type} Request-Example:
        {
            "carouselGroupId":2
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "删除成功",
            "data": []
        }
     *
     *
     */
  async delete() {
    const { app, ctx, service } = this;

    try {
      const res = await service.web.carouselGroup.delete();
      if (res == "id不存在,删除失败") {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule(res);
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule("操作失败");
    }
  }

  /**
     *
     * @api {PUT} /api/carouselGroup/update 修改轮播图分组名
     * @apiName 修改轮播图分组名
     * @apiGroup 轮播图分组模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {String} name 新的轮播图分组名
     * @apiParam  {Number} carouselGroupId 轮播图分组id
     *
     * @apiParamExample  {type} Request-Example:
        {
            "name":"哈哈",
            "carouselGroupId":1
        }
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "修改成功",
            "data": []
        }
     *
     *
     */

  async update() {
    const { app, ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, "name", "id").checkDataRes;

    try {
      if (checkRes) {
        const res = await service.web.carouselGroup.update();
        if (res == "id不存在,修改失败") {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule("操作失败");
    }
  }

  /**
     *
     * @api {GET} /api/carouselGroup/get?carouselGroupId=1 获取某个轮播图分组下所有图片
     * @apiName 获取某个轮播图分组下所有图片
     * @apiGroup 轮播图分组模块
     * @apiVersion  1.0.0
     *
     *
     * @apiParam  {Number} carouselGroupId 轮播图分组id
     *
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "id": 1,        //轮播图分组id
                "name": "哈哈", //轮播图分组名
                "adminId": 1,   //管理员id
                "projectId": 1, //项目id
                "createdAt": "2021-11-11T08:29:02.000Z",
                "updatedAt": "2021-11-11T10:01:08.000Z",
                "Carousels": [
                    {
                        "id": 1,    //轮播图id
                        "url": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",   //轮播图url
                        "carouselGroupId": 1,   //所属轮播图分组id
                        "projectId": 1,     //项目id
                        "createdAt": "2021-11-11T08:29:20.000Z",
                        "updatedAt": "2021-11-11T08:29:20.000Z"
                    },
                    {
                        "id": 2,
                        "url": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
                        "carouselGroupId": 1,
                        "projectId": 1,
                        "createdAt": "2021-11-11T08:29:20.000Z",
                        "updatedAt": "2021-11-11T08:29:20.000Z"
                    }
                ]
            }
        }
     *
     *
     */
  async get() {
    const { ctx, service } = this;
    const checkRes = new ctx.helper.checkData(this, "carouselGroupId")
      .checkDataRes;

    if (checkRes) {
      const res = await service.web.carouselGroup.get();
      return ctx.helper.successModule(res);
    }
  }

  /**
     *
     * @api {GET} /api/carouselGroup/getAllCarouselGroup?limit=10&offset=0 获取所有轮播图分组及对应图片
     * @apiName 获取所有轮播图分组及对应图片
     * @apiGroup 轮播图分组模块
     * @apiVersion  1.0.0
     *
     * @apiParam  {Number} limit 轮播图返回数
     * @apiParam  {Number} offset 轮播图偏移数
     *
     * @apiSuccessExample {type} Success-Response:
        {
            "code": 200,
            "msg": "成功操作",
            "data": {
                "count": 1,     //图片总数
                "rows": [
                    {
                        "id": 1,        //轮播图分组id
                        "name": "哈哈", //轮播图分组名
                        "adminId": 1,   //管理员id
                        "projectId": 1, //项目id
                        "createdAt": "2021-11-11T08:29:02.000Z",
                        "updatedAt": "2021-11-11T10:01:08.000Z",
                        "Carousels": [
                            {
                                "id": 1,    //轮播图id
                                "url": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",   //轮播图url
                                "carouselGroupId": 1,   //所属轮播图分组名
                                "projectId": 1,         //项目id
                                "createdAt": "2021-11-11T08:29:20.000Z",
                                "updatedAt": "2021-11-11T08:29:20.000Z"
                            },
                            {
                                "id": 2,
                                "url": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/_1636424851757_.1.jpg",
                                "carouselGroupId": 1,
                                "projectId": 1,
                                "createdAt": "2021-11-11T08:29:20.000Z",
                                "updatedAt": "2021-11-11T08:29:20.000Z"
                            }
                        ]
                    }
                ]
            }
        }
     *
     *
     */
  async getAllCarouselGroup() {
    const { app, ctx, service } = this;
    const res = await service.web.carouselGroup.getAllCarouselGroup();
    return ctx.helper.successModule(res);
  }
}
module.exports = CarouselGroupController;
